package com.sr.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.sr.entity.Book;
import com.sr.util.BaseDao;

/**
 * BookDaoIml实现类
 * 对数据库表中的数据进行增删查改、原子性操作方法
 *返回结果记录集
 */

public class BookDao {
	/*
	 * 增加图书方法  这个方法不需要验证
	 */
	
	public boolean addBook(Book b) {//Book是实体类名
		try {
			Connection con=BaseDao.getConnection();
			PreparedStatement pstm=con.prepareStatement("insert into book(bookName,author) values(?,?)");
			pstm.setString(1, b.getBookName());
			pstm.setString(2, b.getAuthor());
			int rs=pstm.executeUpdate();
			if(rs>0) {
				return true;
			}else {
				return false;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}
	

	/*
	 * 查询所有图书方法  这个方法需要验证一下
	 */
	public  List<Book> findAllBook() {
		List<Book> list=new ArrayList<Book>();//初始实例一个图书列表
		PreparedStatement ps=null;
			try {
					Connection conn=BaseDao.getConnection();
					ps=conn.prepareStatement("select * from book");
					ResultSet rs=ps.executeQuery();
					Book b=null;
					while(rs.next()) {
								b=new Book();
								b.setbId(rs.getInt(1));	
								b.setBookName(rs.getString(2));
								b.setAuthor(rs.getString(3));

								list.add(b);
								System.out.println(list);
					}	
					   			return list; 
					} catch (SQLException e) {
								e.printStackTrace();					
			}	
					return null;
	}
	/*
	 * 修改商品方法
	 */
	public boolean updateBookById(Book book){
		try{
			Connection con=BaseDao.getConnection();
			PreparedStatement pstm=con.prepareStatement("update book set bookName=? ,author=? where id=?");
			pstm.setString(1, book.getBookName());
			pstm.setString(2, book.getAuthor());
			pstm.setInt(3, book.getbId());
            int rs = pstm.executeUpdate();
            System.out.println("aaaaaaa"+rs);
            if(rs>0){
				return true;
			}else{
				return false;
			}
			}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	/*
	 * 根据id查询相应图书 这一步 是为了通过ID修改  写了这个 给service层调用
	 */		 
public Book findBookById(int id) {
try {
	   Connection con=BaseDao.getConnection();	
	   PreparedStatement pstm=con.prepareStatement("select * from book where id=?");
	   pstm.setInt(1, id);//给他一个id值
	   ResultSet rs=pstm.executeQuery();
	   Book b=null;	
	   while(rs.next()){	
		   		b=new Book();//第二种写法
		   		b.setbId(rs.getInt(1));	
		   		b.setBookName(rs.getString(2));
		   		b.setAuthor(rs.getString(3));
		   		
	   	}
	   			return b;
		} catch (Exception e) {
				return null;
		}
	
}
/*
 * 用户：根据bookName查询相应图书 
 */
public Book findBookByName(String bookName){
	try{
		Connection con=BaseDao.getConnection();
		PreparedStatement pstm=con.prepareStatement("select * from book where bookName=?");
		pstm.setString(1, bookName);
		ResultSet rs=pstm.executeQuery();
		Book b=null;
		while(rs.next()){
			b=new Book();
			b.setbId(rs.getInt(1));
			b.setBookName(rs.getString(2));
			b.setAuthor(rs.getString(3));
			b.setImg(rs.getString(4));
		}
		return b;
	}catch(Exception e){
		e.printStackTrace();
		return null;
	}
}
	/*
	 * 验证该图书是否存在<根据bookName>
	 */
	public boolean isExist(String bookName) {
		return findBookByName(bookName)==null?false:true;
}
	/*
	 * 验证该图书是否存在<根据id>
	 */
	public boolean isExist(int id) {
		return findBookById(id)==null?false:true;
}
	/*
	 * 删除商品方法<根据id>
	 */
	public boolean deleteBookById(int id) {
				try {
					Connection con=BaseDao.getConnection();
					PreparedStatement pstm=con.prepareStatement("delete from book where id=?");
					pstm.setInt(1, id);
					int rs=pstm.executeUpdate();
					if(rs>0) {
						return true;
					}else {
						return false;
					}
				} catch (Exception e) {
						e.printStackTrace();
						return false;
				}
				
	}

}
